/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.core.plugins.impl;

import foo.bar.blog.core.domain.User;
import foo.bar.blog.core.domain.Role;
import foo.bar.blog.core.plugins.UserRegistrationPlugin;
import foo.bar.blog.core.services.UserService;
import foo.bar.blog.core.security.RoleType;
import net.sourceforge.stripes.action.LocalizableMessage;
import net.sourceforge.stripes.action.Message;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrors;

import java.util.List;

/**
 * The default implementation of {@link foo.bar.blog.core.plugins.UserRegistrationPlugin}.
 * <p/>
 * Whenever a {@link User} is registered it automatically enabled it.
 * <p/>
 * An alternative implementation would be to send out an activation email to the
 * {@link foo.bar.blog.core.domain.User}'s email address that requires user interaction
 * before activating the account.
 *
 * @See {@link UserRegistrationPlugin}
 *
 * @author tmjee
 * @version $Date: 2009-06-16 06:56:28 +0000 (Tue, 16 Jun 2009) $ $Id$
 */
public class AutomaticallyEnabledUserRegistration implements UserRegistrationPlugin {

    private UserService userService;

    public AutomaticallyEnabledUserRegistration(UserService userService) {
        this.userService = userService;    
    }

    public void validate(User user, ValidationErrors validationErrors, List<Message> messages) {
        if (userService.isUsernameExists(user.getUsername())) {
            validationErrors.add("user.username", new LocalizableError("foo.bar.blog.core.plugins.impl.AutomaticallyEnabledUserRegistration.usernameAlreadyExists", new Object[] { user.getUsername() }));
        }
    }

    public void register(User user, List<Message> messages) {

        // Once a user registered, it will automatically have the REGISTERED_USER and MODERATOR role
        // The MODERATOR role will still need the user to be added as member of a particular blog to take
        // effect.

        Role registered_role = new Role();
        registered_role.setType(RoleType.REGISTERED_USER);

        Role moderator_role = new Role();
        moderator_role.setType(RoleType.MODERATOR);

        user.getRoles().add(registered_role);
        user.getRoles().add(moderator_role);
        userService.saveOrUpdateUser(user);
        messages.add(new LocalizableMessage("foo.bar.blog.core.plugins.impl.AutomaticallyEnabledUserRegistration.userRegisteredSuccessfully", new Object[] { user.getUsername() }));
    }
}
